package org.openinsula.vulcano.orm.command;

import static org.junit.Assert.*;

import org.junit.Test;
import org.openinsula.vulcano.core.command.Command;
import org.openinsula.vulcano.core.command.invoker.CommandInvoker;
import org.openinsula.vulcano.orm.InitializingCallback;
import org.openinsula.vulcano.orm.entity.Person;
import org.openinsula.vulcano.orm.entity.Pet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;

@ContextConfiguration
public class InitializingCommandWrapperTestCase extends AbstractJUnit4SpringContextTests {

	@Autowired
	private CommandInvoker commandInvoker;

	@Autowired
	private Person person;

	@Test
	public void testExecute() {

		Command<Person> command = new Command<Person>() {
			private static final long serialVersionUID = 1L;

			@Autowired
			private Person person;

			@Override
			public Person execute() {
				return person;
			}

		};

		InitializingCallback<Person> callback = new InitializingCallback<Person>() {
			private static final long serialVersionUID = 1L;

			@Override
			public void init(final Person obj) {
				obj.getPets().add(new Pet());
			}

		};

		Person person = commandInvoker.invoke(new InitializingCommandWrapper<Person>(command, callback));
		assertSame(this.person, person);
		assertEquals(1, person.getPets().size());
	}

}
